package com.cy.api;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadPoolExecutorTests {
    static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1);

    static ThreadFactory threadFactory = new ThreadFactory() {
        private String prefix = "pool-thread-task";
        private AtomicLong atomicLong = new AtomicLong(1);
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r,prefix+atomicLong.getAndIncrement());
        }
    };

    public static void main(String[] args) {
       ThreadPoolExecutor tPool = new ThreadPoolExecutor(1,2,3, TimeUnit.SECONDS,workQueue,threadFactory,new ThreadPoolExecutor.CallerRunsPolicy());
       tPool.execute(new Runnable() {
           @Override
           public void run() {
               try {
                   Thread.sleep(3000);
               }catch (Exception e){}
           }
       });
       tPool.execute(new Runnable() {
           @Override
           public void run() {
               String task = Thread.currentThread().getName()+"->task01";
               System.out.println(task);
               try {
                   Thread.sleep(3000);
               }catch (Exception e){}
           }
       });

        tPool.execute(new Runnable() {
            @Override
            public void run() {
                String task = Thread.currentThread().getName()+"->task02";
                System.out.println(task);
                try {
                    Thread.sleep(3000);
                }catch (Exception e){}
            }
        });

        tPool.execute(new Runnable() {
            @Override
            public void run() {
                String task = Thread.currentThread().getName()+"->task03";
                System.out.println(task);
                try {
                    Thread.sleep(3000);
                }catch (Exception e){}
            }
        });

        tPool.execute(new Runnable() {
            @Override
            public void run() {
                String task = Thread.currentThread().getName()+"->task04";
                System.out.println(task);
                try {
                    Thread.sleep(3000);
                }catch (Exception e){}
            }
        });
    }
}
